/*
 * Copyright (c) 2009, Jan Pastor. All Rights Reserved.
 */

package sk.jp.commons.ui;

import java.awt.*;
import javax.swing.*;
import javax.swing.border.Border;

/**
 * Status bar component designed for displaying info messages on the bottom of
 * window.
 * <p>Additionally supports a set of control components on the right side of
 * bar. There can be useful empty space ({@link #createSpace()}) and
 * splitter ({@link #createSplitter()}) between components.
 */
public class StatusBar extends JPanel {

    public static final Color BORDER_COLOR = new Color(165, 163, 151);
    public static final Color BG_COLOR = new Color(246, 243, 226);
    public static final Color MESSAGE_COLOR = new Color(51, 51, 82);

    public static final String SPACE = "   ";
    public static final String SPLITTER = "  |  ";

    private JPanel messagePanel;
    private JPanel controlsPanel;
    private JComponent messageComponent = createSpace();

    //----------------------------------------------------------------------

    /**
     * Initialize status bar with defaults.
     */
    public StatusBar() {
        init();
    }

    //----------------------------------------------------------------------

    /**
     * Sets message text to status bar.
     * @param message message to present.
     */
    public void setMessage(String message){
        JLabel messageLabel = new JLabel(message);
        messageLabel.setForeground(MESSAGE_COLOR);
        setMessage(messageLabel);
    }

    /**
     * Sets message component to left-side of status bar (removes previous!).
     * @param messageComponent message component.
     */
    public void setMessage(JComponent messageComponent){
        messagePanel.remove(this.messageComponent);
        this.messageComponent = messageComponent;
        messagePanel.add(this.messageComponent);
        validate();
    }

    /**
     * Sets controls to right-side of status bar (removes previous before!).
     * @param components components to set.
     */
    public void setControls(Component... components){
        controlsPanel.removeAll();
        for (Component component : components) {
            controlsPanel.add(component);
        }
        controlsPanel.add(createSpace());
        validate();
    }

    /**
     * Creates empty space in status bar.
     * @return empty space.
     */
    public static JComponent createSpace(){
        return new JLabel(SPACE);
    }

    /**
     * Creates splitter for status bar components.
     * @return splitter between components.
     */
    public static JLabel createSplitter(){
        JLabel label = new JLabel(SPLITTER);
        label.setForeground(BORDER_COLOR);
        return label;
    }

    //----------------------------------------------------------------------

    private void init(){
        BoxLayout layout = new BoxLayout(this, BoxLayout.X_AXIS);
        setLayout(layout);
        setBackground(BG_COLOR);
        Border border = BorderFactory.createLineBorder(BORDER_COLOR, 1);
        setBorder(border);

        FlowLayout flowLayout = new FlowLayout(FlowLayout.LEFT);
        flowLayout.setHgap(0);
        flowLayout.setVgap(2);
        messagePanel = new JPanel(flowLayout);
        messagePanel.setBackground(BG_COLOR);
        messagePanel.add(createSpace());
        add(messagePanel);

        flowLayout = new FlowLayout(FlowLayout.RIGHT);
        flowLayout.setHgap(0);
        flowLayout.setVgap(2);
        controlsPanel = new JPanel(flowLayout);
        controlsPanel.setBackground(BG_COLOR);
        add(controlsPanel);
    }

}
